package com.game.manager
{
	import com.game.element.BigZombieElement;
	import com.game.element.Bullet;
	import com.game.element.GunPack;
	import com.game.element.ItemElement;
	import com.game.element.PlayerElement;
	import com.game.element.SpeedBuff;
	import com.game.element.ZhiLiaoYaoShui;
	import com.game.element.ZombieElement;
	import com.interfaces.game.GlobalAPI;
	import com.interfaces.general.mangaer.ITick;
	import com.interfaces.tool.CommonUtil;
	
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	
	public class ActionManager implements ITick
	{
		public static var _allBullets:Array = [];
		public static var _allAnmy:Array = [];
		public static var _allItem:Array = [];
		
		public static var _instance:ActionManager;
		public var addBossFlag:Boolean = false;
		
		public function ActionManager()
		{
		}
		
		public function exce():void
		{
			var zombieElement:ZombieElement
			
			checkBullets(_allBullets);
			checkBullets(_allAnmy);
			
			for ( var key:* in _allItem){
				var item:ItemElement = _allItem[key];
				if(item.hitTestObject(LayerManager.sceneView.playerView)){
					var flag:Boolean = item.useing(LayerManager.sceneView.playerView);
					if(flag == false){
						break;
					}
					item.dispose();
					CommonUtil.delFromArr2(key, _allItem);
				}
			}
			
			for each(var bullet:Bullet in _allBullets){
				for each(zombieElement in _allAnmy){
					if (bullet.hitTestObject(zombieElement)){
						var leftHp:Number = zombieElement.reduceHp( bullet.damage,bullet.fireMan,bullet.impactForce);
						if(leftHp <=0){
							zombieElement.dispose();
							var ra:Number = Math.random();
							
							if(ra<0.3){
								var zhiLiaoYaoShui:ZhiLiaoYaoShui = new ZhiLiaoYaoShui;
								zhiLiaoYaoShui.x = zombieElement.x;
								zhiLiaoYaoShui.y = zombieElement.y;
								LayerManager.sceneView.addChild(zhiLiaoYaoShui);
								_allItem.push(zhiLiaoYaoShui);
							}else if(ra<0.5){
								var gunPack:GunPack = new GunPack;
								gunPack.x = zombieElement.x;
								gunPack.y = zombieElement.y;
								LayerManager.sceneView.addChild(gunPack);
								_allItem.push(gunPack);
							}
							LayerManager.sceneView.playerView.killedCnt ++;
						}else{
							zombieElement.hpBar.setValue(zombieElement.hp,zombieElement.maxHp);
						}
						
						bullet.dispose();
						break;
					}
				}
			}
			var playerView:PlayerElement =LayerManager.sceneView.playerView
			for each(zombieElement in _allAnmy){
				if (zombieElement.hitTestObject(playerView)){
					if(zombieElement.isCanAtk() == false){
						continue;
					}
					
					playerView.hp -= zombieElement.damage;
					if(playerView.hp <=0){
						trace("game over");
						playerView.x = Math.random()*760;
						playerView.y = Math.random()*600;
						
						playerView.hp = playerView.maxHp;
					}
					playerView.hpBar.setValue(playerView.hp,playerView.maxHp);
					
				}
			}
			
			
			
			var cnt:int = 0;
			for each(var cell:* in _allAnmy){
				cnt++;
			}
			if(cnt < 3 && LayerManager.sceneView.playerView.killedCnt <7){
				zombieElement = new ZombieElement;
				zombieElement.x = Math.random()*760;
				zombieElement.y = Math.random()*600;
				LayerManager.sceneView.addChildAt(zombieElement,0);
				GlobalAPI.tickManager.regeditTick(zombieElement);
				_allAnmy.push(zombieElement);
			}
			
			if(addBossFlag == false && LayerManager.sceneView.playerView.killedCnt == 8){
				addBossFlag = true;
				zombieElement = new BigZombieElement;
				zombieElement.x = Math.random()*760;
				zombieElement.y = Math.random()*600;
				LayerManager.sceneView.addChildAt(zombieElement,0);
				GlobalAPI.tickManager.regeditTick(zombieElement);
				_allAnmy.push(zombieElement);
			}
			
		}
		
		private static function checkBullets(arr:Array):Array{
			var flag:Boolean = false;
			for (var key:* in arr){
				var bullet:DisplayObject = arr[key];
				if(bullet.parent == null){
					CommonUtil.delFromArr(bullet,arr);
					flag = true;
					break;
				}
			}
			if(flag){
				return checkBullets(arr);
			}else{
				return arr;
			}
		}
		
		public function set tickPriority(priority:int):void
		{
		}
		
		public function get tickPriority():int
		{
			return 0;
		}
		
		public static function init(stage:Stage):void{
			ActionManager._instance = new ActionManager;
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
			GlobalAPI.tickManager.regeditTick(ActionManager._instance);
		}
		
		private static function keyDownHandler(e:KeyboardEvent):void{
//			trace(e.keyCode);//87 w
			//87 w
			//83 s
			//65 a
			//68 d
			
			switch(e.keyCode){
				case 87:
					LayerManager.sceneView.playerView.up = true;
					break;
				case 83:
					LayerManager.sceneView.playerView.down = true;
					break;
				case 65:
					LayerManager.sceneView.playerView.left = true;
					break;
				case 68:
					LayerManager.sceneView.playerView.right = true;
					break;
				
				case 82://r
					LayerManager.sceneView.playerView.reloadClip(LayerManager.sceneView.playerView.baseWeapon.reloadNeedTime);
					break;
				case 70://f
					LayerManager.uiMainView.skill_uic.useing();
					break;
			}
		}
		
		private static function keyUpHandler(e:KeyboardEvent):void{
//			trace(e.keyCode);
			
			switch(e.keyCode){
				case 87:
					LayerManager.sceneView.playerView.up = false;
					break;
				case 83:
					LayerManager.sceneView.playerView.down = false;
					break;
				case 65:
					LayerManager.sceneView.playerView.left = false;
					break;
				case 68:
					LayerManager.sceneView.playerView.right = false;
					break;
			}
		}
	}
}